package com.tange.ai.iot.client.gnssa.token

import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import com.tange.ai.iot.client.gnssa.databinding.ActivityInternalAccountModifyBinding
import com.tange.base.toolkit.Tips
import com.tange.core.builtin.account.BuiltinAccount

/**
 * 使用探鸽账号体系，进行密码修改
 */
class InternalAccountModifyActivity: AppCompatActivity() {

    private lateinit var binding: ActivityInternalAccountModifyBinding


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityInternalAccountModifyBinding.inflate(layoutInflater).apply {

            setContentView(root)
            supportActionBar?.hide()

            submit.setOnClickListener {
                modifyPassword()
            }
        }

        window.apply {
            var uiFlags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            uiFlags = uiFlags or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
            addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            statusBarColor = Color.TRANSPARENT
            decorView.systemUiVisibility = uiFlags
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
            }
        }

        com.tange.ai.iot.client.gnssa.utils.Utils.setStatusBar(this, Color.TRANSPARENT)
    }

    private fun modifyPassword() {

        val oldPwd = binding.oldPwd.text.toString().trim()

        if (TextUtils.isEmpty(oldPwd)) {
            Tips.show("请输入原有密码")
            return
        }

        val newPwd = binding.newPwd.text.toString().trim()

        if (TextUtils.isEmpty(newPwd)) {
            Tips.show("请输入新密码")
            return
        }

        BuiltinAccount.password().modify(oldPwd, newPwd) {
            if (it.success) {
                Tips.show("密码修改成功！")
                finish()
            } else {
                Tips.show("失败： ${it.message}")
            }
        }
    }
}